import axios, { AxiosRequestConfig } from "axios";

// 创建 Axios 实例
const apiClient = axios.create({
  baseURL: "http://172.17.62.81:9988/", // 远程服务器的基础 URL
  timeout: 2000, // 请求超时时间
});

// 定义响应数据的通用类型
export type ApiResponse<T> = T;

// GET 请求
export async function getData<T>(
  endpoint: string,
  config?: AxiosRequestConfig
): Promise<ApiResponse<T>> {
  try {
    const response = await apiClient.get<T>(endpoint, config);
    return response.data;
  } catch (error) {
    throw error;
  }
}

export async function uploadFile<T>(
  endpoint: string,
  data: FormData
): Promise<ApiResponse<T>> {
  try {
    const response = await apiClient.post<T>(endpoint, data);
    return response.data;
  } catch (error) {
    console.error("POST request failed:", error);
    throw error;
  }
}

// POST 请求
export async function postData<T, U>(
  endpoint: string,
  data: U,
  config?: AxiosRequestConfig
): Promise<ApiResponse<T>> {
  try {
    const response = await apiClient.post<T>(endpoint, data, config);
    return response.data;
  } catch (error) {
    throw error;
  }
}
